home *** CD-ROM | disk | FTP | other *** search
- // Xor3Test.cpp
- // Automatically generated from breadboard by NeuroSolutions.
-
- #include "NSLib.h"
-
-
- void saveAllWeights(char *weightsFilePath);
- BOOL networkStopped=FALSE;
- unsigned short int weightFileVersion=100;
- BOOL forceBackpropLearning=FALSE, backpropLearningOn=TRUE;
-
- // Component Construction
- Axon axon1;
- BackAxon backAxon25;
- File file35;
- FullSynapse fullSynapse2;
- BackFullSynapse backFullSynapse26;
- Momentum momentum27;
- TanhAxon tanhAxon6;
- BackTanhAxon backTanhAxon28;
- Momentum momentum29;
- FullSynapse fullSynapse10;
- BackFullSynapse backFullSynapse30;
- Momentum momentum31;
- TanhAxon tanhAxon14;
- BackTanhAxon backTanhAxon32;
- Momentum momentum33;
- L2Criterion l2Criterion18;
- BackCriteriaControl backCriteriaControl34;
- File file36;
- File dataWriter;
-
- int main() {
- srand((unsigned)time(NULL));
-
- // Component Initialization
- axon1.setRows(2);
- backAxon25.setRows(2);
- // This section of code was removed after NeuroSolutions generated the code for the BB
- // (Begin)
- //file35.setFilePath("file35.bin");
- //file35.setMode(READ,BINARY);
- // (End)
-
- // This section of code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- char m_sInFilePath[512];
- printf("Enter the name of the input file:\n");
- scanf("%s",m_sInFilePath);
- file35.setFilePath(m_sInFilePath);
- file35.setMode(READ,ASCII);
- // (End)
-
- file35.setSpatialDimension(2,1);
- momentum27.setDefaultMomentum((NSFloat)0.700000);
- momentum27.setDefaultStepSize((NSFloat)0.250000);
- tanhAxon6.setRows(4);
- backTanhAxon28.setOffset((NSFloat)0.100000);
- backTanhAxon28.setRows(4);
- momentum29.setDefaultMomentum((NSFloat)0.700000);
- momentum29.setDefaultStepSize((NSFloat)0.250000);
- momentum31.setDefaultMomentum((NSFloat)0.700000);
- momentum31.setDefaultStepSize((NSFloat)0.025000);
- tanhAxon14.setRows(1);
- backTanhAxon32.setOffset((NSFloat)0.100000);
- backTanhAxon32.setRows(1);
- momentum33.setDefaultMomentum((NSFloat)0.700000);
- momentum33.setDefaultStepSize((NSFloat)0.025000);
- l2Criterion18.setRows(1);
- backCriteriaControl34.setRows(1);
- file36.setFilePath("file36.bin");
- file36.setMode(READ,BINARY);
- file36.setSpatialDimension(1,1);
- dataWriter.setMode(WRITE,STANDARD);
- dataWriter.setSpatialDimension(1,1);
- FILE *loadStream = fopen("Xor.nsw","r");
- if (!loadStream) {
- fprintf(stderr, "Could not open weight file Xor.nsw");
- exit(1);
- }
- weightFileVersion = getWeightFileVersion(loadStream);
-
- // Load Normalization Coefficients of Files
- file35.loadWeights(seekComponent(loadStream, "File", "file35"),weightFileVersion);
- file36.loadWeights(seekComponent(loadStream, "File", "file36"),weightFileVersion);
-
- // Component Interconnection
- axon1.setPreActivityAccess(&file35);
- backAxon25.setDual(&axon1);
- backFullSynapse26.setDual(&fullSynapse2);
- backTanhAxon28.setDual(&tanhAxon6);
- backFullSynapse30.setDual(&fullSynapse10);
- backTanhAxon32.setDual(&tanhAxon14);
- l2Criterion18.setCostAccess(&dataWriter);
- l2Criterion18.setDesiredAccess(&file36);
- l2Criterion18.setSensitivityTotalsData(NULL);
- l2Criterion18.setSensitivityData(NULL);
- backCriteriaControl34.setDual(&l2Criterion18);
- axon1.setNext(&fullSynapse2);
- backAxon25.setLast(&backFullSynapse26);
- fullSynapse2.setLast(&axon1);
- fullSynapse2.setNext(&tanhAxon6);
- backFullSynapse26.setLast(&backTanhAxon28);
- backFullSynapse26.setNext(&backAxon25);
- tanhAxon6.setLast(&fullSynapse2);
- tanhAxon6.setNext(&fullSynapse10);
- backTanhAxon28.setLast(&backFullSynapse30);
- backTanhAxon28.setNext(&backFullSynapse26);
- fullSynapse10.setLast(&tanhAxon6);
- fullSynapse10.setNext(&tanhAxon14);
- backFullSynapse30.setLast(&backTanhAxon32);
- backFullSynapse30.setNext(&backTanhAxon28);
- tanhAxon14.setLast(&fullSynapse10);
- tanhAxon14.setNext(&l2Criterion18);
- backTanhAxon32.setLast(&backCriteriaControl34);
- backTanhAxon32.setNext(&backFullSynapse30);
- l2Criterion18.setLast(&tanhAxon14);
- backCriteriaControl34.setNext(&backTanhAxon32);
- momentum27.setErrorSoma(&backFullSynapse26);
- momentum29.setErrorSoma(&backTanhAxon28);
- momentum31.setErrorSoma(&backFullSynapse30);
- momentum33.setErrorSoma(&backTanhAxon32);
-
- // Load Axon Weights
- // This section of code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- printf("Do you want to load (l) the weights or use random (r) weights?\n");
- char response[128];
- scanf("%s",response);
- if ((response[0] == 'l') || (response[0] == 'L')) {
- // (End)
- axon1.loadWeights(seekComponent(loadStream, "Axon", "axon1"),weightFileVersion);
- tanhAxon6.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon6"),weightFileVersion);
- tanhAxon14.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon14"),weightFileVersion);
- l2Criterion18.loadWeights(seekComponent(loadStream, "L2Criterion", "l2Criterion18"),weightFileVersion);
-
-
- // Load Synapse Weights
- fullSynapse2.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse2"),weightFileVersion);
- fullSynapse10.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse10"),weightFileVersion);
- momentum27.loadWeights(seekComponent(loadStream, "Momentum", "momentum27"),weightFileVersion);
- momentum29.loadWeights(seekComponent(loadStream, "Momentum", "momentum29"),weightFileVersion);
- momentum31.loadWeights(seekComponent(loadStream, "Momentum", "momentum31"),weightFileVersion);
- momentum33.loadWeights(seekComponent(loadStream, "Momentum", "momentum33"),weightFileVersion);
- // (Begin)
- }
- // (End)
- fclose(loadStream);
-
- // Get Ready to Run Network
- int updateCounter=0;
-
- // Run Network
- // This section of code was written after NeuroSolutions generated the code for the BB
- // (Begin)
- unsigned int numberOfEpochs = 0;
- printf("Enter the number of epochs to train for:\n");
- scanf("%d",&numberOfEpochs);
- for (unsigned int epoch=0; epoch<numberOfEpochs; epoch++) {
- // (End)
- // for (unsigned int epoch=0; epoch<100; epoch++) { (Generated Code)
- for (unsigned int exemplar=0; exemplar<4; exemplar++) {
- axon1.fire();
- if (backpropLearningOn || forceBackpropLearning) {
- backAxon25.backpropStarting();
- backFullSynapse26.backpropStarting();
- backTanhAxon28.backpropStarting();
- backFullSynapse30.backpropStarting();
- backTanhAxon32.backpropStarting();
- backCriteriaControl34.backpropStarting();
- backCriteriaControl34.fire();
- backAxon25.backpropComplete();
- backFullSynapse26.backpropComplete();
- backTanhAxon28.backpropComplete();
- backFullSynapse30.backpropComplete();
- backTanhAxon32.backpropComplete();
- backCriteriaControl34.backpropComplete();
- if (++updateCounter >= 4) {
- NSFloat cost3 = l2Criterion18.reportCost();
- momentum27.updateWeights(&fullSynapse2);
- momentum29.updateWeights(&tanhAxon6);
- momentum31.updateWeights(&fullSynapse10);
- momentum33.updateWeights(&tanhAxon14);
- updateCounter = 0;
- }
- forceBackpropLearning = FALSE;
- }
- if (networkStopped)
- goto ConcludeFiring;
- }
- }
-
- ConcludeFiring:
- saveAllWeights("Xor.nsw");
- l2Criterion18.reportCost();
-
- return 0;
- }
-
- void saveAllWeights(char *weightsFilePath)
- { FILE *saveStream = fopen(weightsFilePath,"w");
- if (!saveStream) {
- fprintf(stderr, "Could not open weight file %s\n", weightsFilePath);
- exit(1);
- }
- fprintf(saveStream,"#NSWeightFileVersion 137\n\n");
- file35.saveWeights(putComponent(saveStream, "File", "file35"));
- file36.saveWeights(putComponent(saveStream, "File", "file36"));
- axon1.saveWeights(putComponent(saveStream, "Axon", "axon1"));
- fullSynapse2.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse2"));
- tanhAxon6.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon6"));
- fullSynapse10.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse10"));
- tanhAxon14.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon14"));
- l2Criterion18.saveWeights(putComponent(saveStream, "L2Criterion", "l2Criterion18"));
- momentum27.saveWeights(putComponent(saveStream, "Momentum", "momentum27"));
- momentum29.saveWeights(putComponent(saveStream, "Momentum", "momentum29"));
- momentum31.saveWeights(putComponent(saveStream, "Momentum", "momentum31"));
- momentum33.saveWeights(putComponent(saveStream, "Momentum", "momentum33"));
- fclose(saveStream);
- }
-
-